package com.shou.lifecollege.framework.Security.core;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.shou.lifecollege.framework.Common.exception.ServiceException;
import com.shou.lifecollege.framework.Security.model.LoginUser;
import com.shou.lifecollege.modules.common.entity.SysRole;
import com.shou.lifecollege.modules.common.entity.SysRolePermission;
import com.shou.lifecollege.modules.common.entity.SysUser;
import com.shou.lifecollege.modules.common.mapper.SysUserMapper;
import com.shou.lifecollege.modules.common.service.SysRolePermissionService;
import com.shou.lifecollege.modules.common.service.SysRoleService;
import com.shou.lifecollege.modules.common.service.SysUserService;
import io.netty.util.internal.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: luotian
 * @Date: 2022/11/24 11:02
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
  @Autowired
    private SysUserService sysUserService;
  @Autowired
    private SysRoleService sysRoleService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<SysUser> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("username",username);
        SysUser user=sysUserService.getOne(userQueryWrapper);
        if(ObjectUtils.isNull(user))
        {
            throw new ServiceException("登录用户："+username+"不存在！");
        }
        List<SysRole> roleList=sysRoleService.getRoleByUserId(user.getUsrid());
        user.setRoleList(roleList);
        return null;
    }
}
